package com.speakglobe.guest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;

import com.speakglobe.panelist.model.Panelist;
import com.speakglobe.panelist.service.PanelistService;
import com.speakglobe.security.model.User;
import com.speakglobe.security.service.UserDetailsService;
import com.speakglobe.util.JSONView;
import com.speakglobe.util.ParamUtil;
import com.speakglobe.util.SPGController;

public class GuestController extends SPGController {
	
	private PanelistService panelistService;
	private UserDetailsService userDetailsService;
	
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String errorLogin = request.getParameter("error_login");
		
		HashMap<String, Object> model = new HashMap<String, Object>();
		model.put("errorLogin", errorLogin == null ? "" : errorLogin);
		
        return new ModelAndView("/login", model);
    }
	
	public ModelAndView home(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HashMap<String, Object> homeModel = new HashMap<String, Object>();
		homeModel.put("PageTitle", "SpeakGlobe - Home");
		homeModel.put("PageLink", "home");
		homeModel.put("templateType", "home");
		
		return new ModelAndView("/index", homeModel);
	}
	
	/** company page and sub-pages **/
	public ModelAndView company(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HashMap<String, Object> companyModel = new HashMap<String, Object>();
		companyModel.put("PageTitle", "SpeakGlobe - Company");
		companyModel.put("PageLink", "company");
		companyModel.put("templateType", "nonHome");
		
		return new ModelAndView("/company", companyModel);
	}
	
	public ModelAndView aboutUs(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HashMap<String, Object> aboutUsModel = new HashMap<String, Object>();
		aboutUsModel.put("PageTitle", "SpeakGlobe - About Us");
		aboutUsModel.put("PageLink", "company");
		aboutUsModel.put("templateType", "nonHome");
		
		return new ModelAndView("/about_us", aboutUsModel);
	}
	
	public ModelAndView visionMissionAndValues(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HashMap<String, Object> visionMissionAndValuesModel = new HashMap<String, Object>();
		visionMissionAndValuesModel.put("PageTitle", "SpeakGlobe - Vision, Mission and Values");
		visionMissionAndValuesModel.put("PageLink", "company");
		visionMissionAndValuesModel.put("templateType", "nonHome");
		
		return new ModelAndView("/vision_mission_and_values", visionMissionAndValuesModel);
	}
	
	public ModelAndView corporateMessage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HashMap<String, Object> corporateMessageModel = new HashMap<String, Object>();
		corporateMessageModel.put("PageTitle", "SpeakGlobe - Corporate Message");
		corporateMessageModel.put("PageLink", "company");
		corporateMessageModel.put("templateType", "nonHome");
		
		return new ModelAndView("/corporate_message", corporateMessageModel);
	}
	
	public ModelAndView termsAndConditions(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HashMap<String, Object> termsAndConditionsModel = new HashMap<String, Object>();
		termsAndConditionsModel.put("PageTitle", "SpeakGlobe - Terms and Conditions");
		termsAndConditionsModel.put("PageLink", "company");
		termsAndConditionsModel.put("templateType", "nonHome");
		
		return new ModelAndView("/terms_and_conditions", termsAndConditionsModel);
	}
	
	public ModelAndView legalDocument(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HashMap<String, Object> legalDocumentModel = new HashMap<String, Object>();
		legalDocumentModel.put("PageTitle", "SpeakGlobe - Legal Document");
		legalDocumentModel.put("PageLink", "company");
		legalDocumentModel.put("templateType", "nonHome");
		
		return new ModelAndView("/legal_document", legalDocumentModel);
	}
	
	/** company page and sub-pages **/
	
	public ModelAndView community(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HashMap<String, Object> communityModel = new HashMap<String, Object>();
		communityModel.put("PageTitle", "SpeakGlobe - Community");
		communityModel.put("PageLink", "community");
		communityModel.put("templateType", "nonHome");
		
		return new ModelAndView("/community", communityModel);
	}
	
	public ModelAndView compensationPlan(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HashMap<String, Object> compensationPlanModel = new HashMap<String, Object>();
		compensationPlanModel.put("PageTitle", "SpeakGlobe - Compensation Plan");
		compensationPlanModel.put("PageLink", "compensationPlan");
		compensationPlanModel.put("templateType", "nonHome");
		
		return new ModelAndView("/compensation_plan", compensationPlanModel);
	}
	
	public ModelAndView mediaCenter(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HashMap<String, Object> mediaCenterModel = new HashMap<String, Object>();
		mediaCenterModel.put("PageTitle", "SpeakGlobe - Media Center");
		mediaCenterModel.put("PageLink", "mediaCenter");
		mediaCenterModel.put("templateType", "nonHome");
		
		return new ModelAndView("/media_center", mediaCenterModel);
	}
	
	public ModelAndView joinNow(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HashMap<String, Object> joinNowModel = new HashMap<String, Object>();
		joinNowModel.put("PageTitle", "SpeakGlobe - Join Now");
		joinNowModel.put("PageLink", "joinNow");
		joinNowModel.put("templateType", "nonHome");
		
		return new ModelAndView("/join_now", joinNowModel);
	}
	
	public ModelAndView contacts(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HashMap<String, Object> contactsModel = new HashMap<String, Object>();
		contactsModel.put("PageTitle", "SpeakGlobe - Contact Us");
		contactsModel.put("PageLink", "contacts");
		contactsModel.put("templateType", "nonHome");
		
		return new ModelAndView("/contact_us", contactsModel);
	}
	
	public ModelAndView register(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HashMap<String, Object> panelistRegistrationModel = new HashMap<String, Object>();
		panelistRegistrationModel.put("PageTitle", "SpeakGlobe - Contact Us");
		panelistRegistrationModel.put("PageLink", "contacts");
		panelistRegistrationModel.put("templateType", "nonHome");
		panelistRegistrationModel.put("showSponsorDetail", false);
		panelistRegistrationModel.put("sponsorNotFound", false);
		
		/*if(!ParamUtil.exists("sponsor")){
			panelistRegistrationModel.put("showSponsorSearch", true);
			panelistRegistrationModel.put("showUserInputForm", false);
			panelistRegistrationModel.put("showPaymentGateway", false);
		} else if(ParamUtil.exists("sponsor")){
			panelistRegistrationModel.put("showSponsorSearch", false);
			panelistRegistrationModel.put("showUserInputForm", true);
			panelistRegistrationModel.put("showPaymentGateway", false);
		} */
		
		return new ModelAndView("/registration", panelistRegistrationModel);
	}
	
	public ModelAndView searchSponsor(HttpServletRequest request, HttpServletResponse response) throws Exception {
//		Map<String, String> searchMap = null;
//		List<Map<String, String>> sponsorList = new ArrayList<Map<String, String>>();
//		Map<String, List<Map<String, String>>> returnMap = new HashMap<String, List<Map<String, String>>>();	
		String keyword = ParamUtil.getString(request, "keyword"); 
		System.out.println("KEYWORD: "+keyword);
		Panelist sponsor = panelistService.searchPanelistByKeyword(keyword);
		Map<String, Object> result = new HashMap<String, Object>();
		if(sponsor != null){
			result.put("name", sponsor.getUsername());
			result.put("showSponsorDetail", true);
			result.put("sponsorNotFound", false);
		}else{
			result.put("name", "not found");
			result.put("showSponsorDetail", false);
			result.put("sponsorNotFound", true);
		}
		
		return new ModelAndView(new JSONView(), result);
	}
	
	public ModelAndView createDefaultUser(HttpServletRequest request, HttpServletResponse response) throws Exception{
		User panelist = new Panelist();
		panelist.setUsername("root");
		panelist.setEncryptPassword("root", "root");
		panelist.setUserRole(User.ROLE_PANELIST);
		
		userDetailsService.save(panelist);	
		
		return null;
	}

	public PanelistService getPanelistService() {
		return panelistService;
	}

	public void setPanelistService(PanelistService panelistService) {
		this.panelistService = panelistService;
	}

	public void setUserDetailsService(UserDetailsService userDetailsService) {
		this.userDetailsService = userDetailsService;
	}
}
